﻿using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;
using VisionCloud.Service.Ms;

namespace VisionCloud.Web.Middleware.ProxyRewrite
{
    public class PrefixRewriter : IUrlRewriter
    {
        private readonly PathString _prefix; //前缀值
        private readonly string _newHost; //转发的地址

        public PrefixRewriter(PathString prefix, string newHost)
        {
            _prefix = prefix;
            _newHost = newHost;
        }

        /// <summary>
        /// rewite  uri
        /// </summary>
        /// <param name="context">HttpContext </param>
        /// <returns></returns>
        public Task<Uri> RewriteUri(HttpContext context)
        {
            MSOSSService ms = new MSOSSService();
            FileRequestRewriteEntity frre = ms.GetObjectRequestURL(context.Request.Path.Value);
            // ms.GetObjectAsync("task-dotcloud-zip", "蔡工Test111-1栋-1层-初测-结构工程-巡检.zip");
            if (frre.IsNeedToRewrite)
            {
                var targetUri = frre.TargetFilePath;
                return Task.FromResult(new Uri(targetUri));
            }
            else
            {
                return Task.FromResult((Uri)null);
            }
        }
    }
}